#include "Administrator.h"
#include "ui_administrator_spells.h"

Administrator::Administrator()
{
    insertData = new InsertDataBase;
    selectData = new SelectDataBase;
}

Administrator::~Administrator()
{

}


void Administrator::majComboEffect(QList<QString> list)
{
    ui->comboEffect->clear();
    ui->comboEffect->addItem("-");
    for(int i = 0; i < list.size(); ++i)
        ui->comboEffect->addItem(list.at(i));
}


void Administrator::displaySpellsAdmin()
{
    ui = new Ui::AdministratorSpells;
    ui->setupUi(this);
    connect(ui->buttonAddElem, SIGNAL(clicked()), this, SLOT(addElem()));
    connect(ui->buttonAddClass, SIGNAL(clicked()), this, SLOT(addClass()));
    connect(ui->buttonAddEffect, SIGNAL(clicked()), this, SLOT(addEffect()));
    connect(ui->buttonAddRace, SIGNAL(clicked()), this, SLOT(addRace()));
    connect(ui->buttonAddSpell, SIGNAL(clicked()), this, SLOT(addSpell()));
    majComboEffect(selectData->selectAllEffects());
}

void Administrator::addElem()
{
    insertData->insertElement(ui->lineElemName->text(), ui->textDescripElem->toPlainText());
    ui->lineElemName->setText("");
    ui->textDescripElem->setPlainText("");
}

void Administrator::addClass()
{
    insertData->insertClass(ui->lineNameClass->text(), ui->spinCoefHPClass->value(), ui->spinCoefManaClass->value());
    ui->lineNameClass->setText("");
    ui->spinCoefHPClass->setValue(0);
    ui->spinCoefManaClass->setValue(0);
}

void Administrator::addEffect()
{
    insertData->insertEffect(ui->lineEffectName->text(), ui->textDescripEffect->toPlainText());
    ui->lineEffectName->setText("");
    ui->textDescripEffect->setPlainText("");
    majComboEffect(selectData->selectAllEffects());
}

void Administrator::addRace()
{
    insertData->insertRace(ui->lineNameRace->text(), ui->spinCoefHPRace->value(), ui->spinCoefManaRace->value());
    ui->lineNameRace->setText("");
    ui->spinCoefHPRace->setValue(0);
    ui->spinCoefManaRace->setValue(0);
}

void Administrator::addSpell()
{
    QString *str = new QString;
    int deg_rand=0;
    generateStringD(str, &deg_rand);
    insertData->insertSpell(ui->lineNameSpell->text(), ui->spinMinDeg->value(), deg_rand, *str, 0, ui->spinInc->value(), ui->spinConsoMana->value());
    ui->lineNameSpell->setText("");
    ui->spinMinDeg->setValue(0);
    ui->spinInc->setValue(0);
    ui->spinConsoMana->setValue(0);
    free(str);
}

void Administrator::generateStringD(QString *str, int *deg_r)
{
    if(ui->checkD4->isChecked())
    {
        *str += QString::number(ui->spind4->value())+"D4-";
        *deg_r += ui->spind4->value()*4;
    }
    if(ui->checkD6->isChecked())
    {
        *str += QString::number(ui->spind6->value())+"D6-";
        *deg_r += ui->spind6->value()*6;
    }
    if(ui->checkD8->isChecked())
    {
        *str += QString::number(ui->spind8->value())+"D8-";
        *deg_r += ui->spind8->value()*8;
    }
    if(ui->checkD10->isChecked())
    {
        *str += QString::number(ui->spind10->value())+"D10-";
        *deg_r += ui->spind10->value()*10;
    }
    if(ui->checkD12->isChecked())
    {
        *str += QString::number(ui->spind12->value())+"D12-";
        *deg_r += ui->spind12->value()*12;
    }
    if(ui->checkD20->isChecked())
    {
        *str += QString::number(ui->spind20->value())+"D20-";
        *deg_r += ui->spind20->value()*20;
    }
    if(ui->checkD100->isChecked())
    {
        *str += QString::number(ui->spind100->value())+"D100-";
        *deg_r += ui->spind100->value()*100;
    }

}
